package sample.crawler;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import sample.model.Log;

import java.io.IOException;

public class LogTask implements Runnable {

    private Log log;

    public LogTask(Log log) {
        this.log = log;
    }

    @Override
    public void run() {
        HttpPost post = new HttpPost("http://localhost:8081/crawler-api/api/log/save");
        HttpEntity entity = new StringEntity(JSONObject.toJSONString(log),"UTF-8");
        post.setEntity(entity);
        post.setHeader("Content-Type","application/json;charset=UTF-8");

        CloseableHttpClient httpClient = HttpClients.custom().disableAutomaticRetries().build();
        try {
            httpClient.execute(post);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
